(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.popup.txnJournal.listController", listController);
	listController.$inject = [ "$scope", "$uibModalInstance", "dialogsModule", "gridService", "layoutConfig", "validatorConfig", "authService" ];
	function listController($scope, $uibModalInstance, dialogsModule, gridService, layoutConfig, validatorConfig, authService) {
		var ctrl = this;
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.searchCriteria = ctrl.searchCriteria || {};

		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["close"] || dialogsModule.btnCallback["close"]() !== false) {
				$uibModalInstance.dismiss();
			}
		};

		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, rowSelectionChanged, sortChanged);
			ctrl.innerGridOptions.data = "vm.searchCriteria.list";
			if (!ctrl.dialogsModule.params.txnId) {
				return;
			}
			ctrl.searchCriteria = angular.merge(ctrl.searchCriteria, ctrl.dialogsModule.params);
			ctrl.search();
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.innerGridOptions);
			}, angular.noop);
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : "";
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : "";
			ctrl.search();
		}

	}
})(angular);